package com.mk.admin.service.impl;

import com.mk.admin.mapper.RRoleSysMenuMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.model.dto.RRoleSysMenuDto;
import com.mk.admin.model.entity.RRoleSysMenuEntity;
import com.mk.admin.model.vo.RRoleSysMenuVo;
import com.mk.admin.service.RRoleSysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RRoleSysMenuServiceImpl implements RRoleSysMenuService {
    @Autowired
    private RRoleSysMenuMapper rRoleSysMenuMapper;

    @Override
    public ResultDto addBatch(List<RRoleSysMenuVo> list) {
        if (list.size() == 0) {
            return ResultDto.data("error", "没有增加的角色关联");
        }
//        增加角色和菜单关联关系时，先删除当前角色对应的菜单
        RRoleSysMenuEntity entity = new RRoleSysMenuEntity();
        entity.setRoleId(list.get(0).getRoleId());
        List<RRoleSysMenuDto> select = rRoleSysMenuMapper.select(entity);
        List<Integer> ids = new ArrayList<>();
        select.forEach(item -> ids.add(item.getId()));
        rRoleSysMenuMapper.deleteBatch(ids);
        return ResultDto.data(rRoleSysMenuMapper.addBatch(list));
    }

    @Override
    public ResultDto deleteBatch(List<Integer> list) {
        return ResultDto.data(rRoleSysMenuMapper.deleteBatch(list));
    }
}
